<template>
  <n-drawer
    v-model:show="isDrawer"
    :width="width"
    :placement="placement"
    :auto-focus="false"
    @after-leave="handleReset"
  >
    <n-drawer-content :title="title" closable>
      <n-form
        :model="formParams"
        :rules="rules"
        ref="formRef"
        label-placement="left"
        :label-width="80"
      >
        <n-form-item label="配置名称" path="config_name">
          <n-input placeholder="请输入配置名称" v-model:value="formParams.config_name" />
        </n-form-item>
        <n-form-item label="配置编码" path="config_code">
          <n-input placeholder="请输入配置编码" v-model:value="formParams.config_code" />
        </n-form-item>
        <n-form-item label="配置值" path="config_value">
          <n-input placeholder="请输入配置值" v-model:value="formParams.config_value" />
        </n-form-item>
        <n-form-item label="描述" path="configDesc">
          <n-input
            type="textarea"
            placeholder="请输入配置描述"
            v-model:value="formParams.config_desc"
          />
        </n-form-item>
        <n-form-item label="排序" path="configOrder">
          <n-input-number placeholder="显示排序" v-model:value="formParams.config_order" />
        </n-form-item>
      </n-form>
      <template #footer>
        <n-space>
          <n-button @click="handleReset">重置</n-button>
          <n-button type="primary" :loading="subLoading" @click="formSubmit">提交</n-button>
        </n-space>
      </template>
    </n-drawer-content>
  </n-drawer>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { useMessage } from 'naive-ui';
  import type { formParamsType } from './types';

  import { addConfig, editConfig, configInfo } from '@/api/system/config';

  const rules = {
    config_name: {
      required: true,
      message: '配置名称不能为空',
      trigger: 'blur',
    },
    config_code: {
      required: true,
      message: '配置编码不能为空',
      trigger: 'blur',
    },
    config_value: {
      required: true,
      message: '配置值不能为空',
      trigger: 'blur',
    },
  };

  const emit = defineEmits(['change']);

  defineProps({
    title: {
      type: String,
      default: '添加配置',
    },
    width: {
      type: Number,
      default: 450,
    },
    permissionList: {
      type: Array,
    },
  });

  const defaultValueRef = () => ({
    id: null,
    config_code: '',
    config_desc: '',
    config_name: '',
    config_order: null,
    config_value: '',
  });

  const message = useMessage();
  const formRef: any = ref(null);
  const isDrawer = ref(false);
  const subLoading = ref(false);

  const formParams = ref<formParamsType>(defaultValueRef());

  const placement = 'right';

  function openDrawer(id?) {
    if (id) {
      formParams.value.id = id;
      getInfo();
      return;
    }
    isDrawer.value = true;
  }

  function closeDrawer() {
    isDrawer.value = false;
  }

  function formSubmit() {
    formRef.value.validate((errors) => {
      if (!errors) {
        if (formParams.value.id) {
          editConfig(formParams.value).then((_) => {
            message.success('编辑成功');
            emit('change');
            handleReset();
            closeDrawer();
          });
        } else {
          addConfig(formParams.value).then((_) => {
            message.success('添加成功');
            emit('change');
            handleReset();
            closeDrawer();
          });
        }
      } else {
        message.error('请填写完整信息');
      }
    });
  }

  function handleReset() {
    formRef.value.restoreValidation();
    formParams.value = Object.assign(formParams.value, defaultValueRef());
  }

  function getInfo() {
    configInfo({ id: formParams.value.id }).then((res) => {
      const info = {
        id: res.id,
        config_code: res.config_code,
        config_desc: res.config_desc,
        config_name: res.config_name,
        config_order: res.config_order,
        config_value: res.config_value,
      };
      formParams.value = info;
      isDrawer.value = true;
    });
  }

  onMounted(() => {});

  defineExpose({
    openDrawer,
    closeDrawer,
  });
</script>
